package com.youlai.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.system.common.result.PageResult;
import com.youlai.system.common.result.Result;
import com.youlai.system.model.entity.Product;
import com.youlai.system.model.form.ProductFrom;
import com.youlai.system.model.query.ProductPageQuery;
import com.youlai.system.model.vo.ProductPageVo;
import com.youlai.system.service.ProductCategoryService;
import com.youlai.system.service.ProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @version 1.0
 * @Author zzps
 * @Date 2024/3/11 22:23
 * @注释
 */
@Tag(name = "商品管理相关接口")
@RestController
@RequestMapping("/api/product")
public class ProductController {
    @Resource
    private ProductService productService;

    @Operation(summary = "分页查询商品列表")
    @GetMapping("/list")
    public PageResult<ProductPageVo> list(@ParameterObject ProductPageQuery productPageQuery) {
        IPage<ProductPageVo> productList= productService.getProductPageList(productPageQuery);
        return PageResult.success(productList);
    }

    @Operation(summary = "新增商品")
    @PostMapping("/add")
    public Result<Void> add(@RequestBody @Valid ProductFrom productForm) {
        boolean result =  productService.addProduct(productForm);
        return Result.judge(result);
    }

    @Operation(summary = "修改商品")
    @PutMapping("/update")
    public Result<Void> update(@RequestBody @Valid ProductFrom productForm) {
        boolean result =  productService.updateProduct(productForm);
        return Result.judge(result);
    }

    @Operation(summary = "删除商品")
    @DeleteMapping("/delete/{id}")
    public Result<Void> delete(@PathVariable Long id) {
        boolean result =  productService.deleteProduct(id);
        return Result.judge(result);
    }

    @Operation(summary = "上下架商品")
    @PutMapping("/upOrDown/{id}/{status}")
    public Result<Void> up(@PathVariable Long id, @PathVariable Integer status) {
        boolean result =  productService.upOrDownProduct(id, status);
        return Result.judge(result);
    }

    @Operation(summary = "根据id查询商品")
    @GetMapping("/{id}")
    public Result<ProductPageVo> getById(@PathVariable Long id) {
        ProductPageVo product = productService.getProductById(id);
        return Result.success(product);
    }

    @Operation(summary = "批量删除商品")
    @DeleteMapping("/batch/delete")
    public Result<Void> batchDelete(@RequestBody List<Long> ids) {
        boolean result =  productService.batchDeleteProduct(ids);
        return Result.judge(result);
    }
}
